﻿using Eleven.BreakAway.DataAccess;
using Eleven.BreakAway.Model;
using System;
using System.Data.Entity;
using System.IO;
using System.Linq;

namespace Eleven.BreakAway.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", Directory.GetCurrentDirectory());
#if DEBUG
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BreakAwayContext>());
#else
            Database.SetInitializer(new CreateDatabaseIfNotExists<BreakAwayContext>());
#endif

            InsertDestination();
            InsertTrip();
            InsertPerson();

            UpdateTrip();
            UpdatePerson();
        }

        private static void InsertDestination()
        {
            var destination = new Destination
            {
                Country = "Indonesia",
                Description = "EcoTourism at its best in exquisite bali",
                Name = "Bali"
            };

            using (var ctx = new BreakAwayContext())
            {
                ctx.Destinations.Add(destination);
                ctx.SaveChanges();
            }
        }

        private static void InsertTrip()
        {
            var now = DateTime.Now;

            var trip = new Trip
            {
                From = now,
                To = now.AddDays(3),
                Cost = 120m
            };

            using (var ctx = new BreakAwayContext())
            {
                ctx.Trips.Add(trip);
                ctx.SaveChanges();
            }
        }

        private static void InsertPerson()
        {
            var person = new Person
            {
                SocialSecurityNumber = "888888888888",
                LastName = "Garnett",
                FirstName = "Kevin"
            };

            using (var ctx = new BreakAwayContext())
            {
                ctx.Persons.Add(person);
                ctx.SaveChanges();
            }
        }

        private static void UpdateTrip()
        {
            using (var ctx = new BreakAwayContext())
            {
                var trip = ctx.Trips.FirstOrDefault();
                trip.Cost = 750m;
                ctx.SaveChanges();
            }
        }

        private static void UpdatePerson()
        {
            using(var ctx = new BreakAwayContext())
            {
                var person = ctx.Persons.FirstOrDefault();
                person.FirstName = "Rowena";
                ctx.SaveChanges();
            }
        }

        private static void DeleteDestination()
        {
            
        }
    }
}
